package com.cuhk.gulimall.product.service.impl;

import com.cuhk.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.cuhk.gulimall.product.dao.AttrDao;
import com.cuhk.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.cuhk.gulimall.product.entity.AttrEntity;
import com.cuhk.gulimall.product.service.AttrAttrgroupRelationService;
import com.cuhk.gulimall.product.service.AttrService;
import com.cuhk.gulimall.product.vo.AttrGroupWithAttrIdVo;
import com.cuhk.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.cuhk.gulimall.product.vo.SkuItemVo;
import com.cuhk.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuhk.common.utils.PageUtils;
import com.cuhk.common.utils.Query;

import com.cuhk.gulimall.product.dao.AttrGroupDao;
import com.cuhk.gulimall.product.entity.AttrGroupEntity;
import com.cuhk.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        // 如果指定了类比ID
        // 则按照类别ID查询
        if(catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)) {
            wrapper.and((w) -> {
                // like()相当于like %cond%
                // 相当于SQL语句中的: attr_group_id = key OR attr_group_name = key
                w.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        IPage<AttrGroupEntity> page = this.page(
                // 这个getPage返回的是分页信息
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        List<AttrGroupEntity> attrGroups = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        List<AttrGroupWithAttrsVo> vos = attrGroups.stream().map((group) -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, vo);
            List<AttrEntity> attrs = attrService.getRelationAttr(group.getAttrGroupId());
            vo.setAttrs(attrs);
            return vo;
        }).collect(Collectors.toList());

        return vos;
    }

    @Override
    public List<AttrGroupEntity> getAttrGroupList(Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catelogId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<AttrGroupWithAttrIdVo> getAttrGroupWithAttrIdsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> groups = this.getAttrGroupList(catelogId);
        List<AttrGroupWithAttrIdVo> vos = new ArrayList<>();
        for(AttrGroupEntity group : groups) {
            AttrGroupWithAttrIdVo vo = new AttrGroupWithAttrIdVo();
            vo.setAttrGroupId(group.getAttrGroupId());
            vo.setAttrGroupName(group.getAttrGroupName());
            List<Long> attrIds = attrAttrgroupRelationService.getAttrIds(group.getAttrGroupId());
            vo.setAttrIds(attrIds);
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<SpuItemAttrGroupVo> getSpuItemAttrGroupVoByIds(Long catelogId, Long spuId) {
        return attrGroupDao.selectSpuItem(catelogId, spuId);
    }

}